﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FileOperatorSpace;
using System.Threading.Tasks;

namespace VideoPlayer {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        public FileOperator fileOperator  = new FileOperator();
        private string mediaPath;
        public Form1() {
            InitializeComponent();
            axWindowsMediaPlayer.enableContextMenu = false;
            axWindowsMediaPlayer.ContextMenuStrip = contextMenuStrip;
            this.KeyPreview = true;
            //设置播放速率
            this.axWindowsMediaPlayer.settings.rate = 1;
        }

        private void Form1_Load(object sender, EventArgs e) {

        }

        private void axWindowsMediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) {
            /*获取当前的当前播放器的状态*/
            int playState = Convert.ToInt16(axWindowsMediaPlayer.playState);
            switch (playState) {
                case 1: //停止
                    axWindowsMediaPlayer.Ctlcontrols.stop();
                    break;
                case 2: //暂停
                    axWindowsMediaPlayer.Ctlcontrols.pause();
                    break;
                case 3://播放
                    axWindowsMediaPlayer.Ctlcontrols.play();
                    break;
                case 6://正在缓冲
                    break;
                case 9://正在连接
                    break;
                case 10://转换就绪
                    axWindowsMediaPlayer.Ctlcontrols.fastReverse();
                    break;
                default:
                    break;
            }
        }

        private void chooseFile_Click(object sender, EventArgs e) {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "所有文件|*.*";
            openFileDialog.FilterIndex = 0;
            openFileDialog.Title = "选择文件";
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK) {
                mediaPath = openFileDialog.FileName;
                Task task = new Task(() => {
                    try {
                        axWindowsMediaPlayer.URL = mediaPath;
                        axWindowsMediaPlayer.Ctlcontrols.play();
                        Console.WriteLine(axWindowsMediaPlayer.currentMedia.duration);
                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                });
                task.Start();
            }
        }

        private void axWindowsMediaPlayer_ClickEvent(object sender, AxWMPLib._WMPOCXEvents_ClickEvent e) {
           
        }

        private void axWindowsMediaPlayer_DoubleClickEvent(object sender, AxWMPLib._WMPOCXEvents_DoubleClickEvent e) {
            contextMenuStrip.Show(Cursor.Position);
        }

        private void closePlayer_Click(object sender, EventArgs e) {
            this.Close();
        }

        private bool playerState = false;
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
            Keys key = keyData;
            if (key == Keys.Space) {
                if (playerState == false) {
                    axWindowsMediaPlayer.Ctlcontrols.pause();
                    playerState = true;
                } else {
                    axWindowsMediaPlayer.Ctlcontrols.play();
                    playerState = false;
                }
            }
            return true;
        }
    }
}
